package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author:YZN
 * @date:2022/12/30
 * @description:
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加品类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R add(@RequestBody Category category) {
        return categoryService.addCategory(category);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R findpage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        return categoryService.findpage(page, pageSize);
    }

    /**
     * 删除品类
     */
    @DeleteMapping
    public R deleteById(@RequestParam(value = "id",required = true) Long id) {
        return categoryService.deleteById(id);
    }

    /**
     * 修改品类
     */
    @PutMapping
    public R updateCategory(@RequestBody Category category){
        return categoryService.updateCategory(category);
    }

    /**
     * 根据类型查询分类
     */
    @GetMapping("/list")
    public R list(@RequestParam(value = "type",required = false) Integer type){
        return categoryService.list(type);
    }

}
